<template>
  <div class="typhoon-container">
    <!-- 台风控制组件 -->
    <div class="typhoon-control" :class="{ expanded: isPanelExpanded }">
      <!-- 控制图标 -->
      <div class="control-icon" @click="togglePanel" v-if="!isPanelExpanded" title="台风示例">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M12 2a10 10 0 0 0-7 3 10 10 0 0 0 0 14 10 10 0 0 0 7 3 10 10 0 0 0 7-3 10 10 0 0 0 0-14 10 10 0 0 0-7-3z"></path>
          <path d="M12 6v12M6 12h12"></path>
          <path d="M4.93 4.93l14.14 14.14M19.07 4.93L4.93 19.07"></path>
        </svg>
      </div>

      <!-- 台风控制面板 -->
      <div class="typhoon-control-panel" v-if="isPanelExpanded">
        <div class="panel-header">
          <h3>台风示例</h3>
          <div class="close-btn" @click="togglePanel">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M18 6L6 18M6 6l12 12"></path>
            </svg>
          </div>
        </div>
        
        <!-- 图例部分 -->
        <div class="legend-section">
          <h4>图例</h4>
          
          <!-- 风圈、登陆点、预报台 -->
          <div class="legend-group">
            <div class="legend-item checkbox-label" @click="toggleShow('showWindCircle')">
              <input type="checkbox" v-model="showOptions.showWindCircle" class="legend-checkbox">
              <span class="checkmark"></span>
              <span class="legend-text">风圈</span>
            </div>
            <div class="legend-item checkbox-label" @click="toggleShow('showLandPoints')">
              <input type="checkbox" v-model="showOptions.showLandPoints" class="legend-checkbox">
              <span class="checkmark"></span>
              <span class="legend-text">登陆点</span>
            </div>
            <div class="legend-item checkbox-label" @click="toggleShow('showForecastPaths')">
              <input type="checkbox" v-model="showOptions.showForecastPaths" class="legend-checkbox">
              <span class="checkmark"></span>
              <span class="legend-text">预报台</span>
            </div>
          </div>
          
          <!-- 预报台 -->
          <div class="legend-group">
            <div class="legend-title">预报台</div>
            <div class="legend-item">
              <span class="legend-line" style="background: linear-gradient(90deg, red 50%, transparent 50%);"></span>
              <span class="legend-text">中国</span>
            </div>
            <div class="legend-item">
              <span class="legend-line" style="background: linear-gradient(90deg, orange 50%, transparent 50%);"></span>
              <span class="legend-text">中国香港</span>
            </div>
            <div class="legend-item">
              <span class="legend-line" style="background: linear-gradient(90deg, green 50%, transparent 50%);"></span>
              <span class="legend-text">日本</span>
            </div>
            <div class="legend-item">
              <span class="legend-line" style="background: linear-gradient(90deg, purple 50%, transparent 50%);"></span>
              <span class="legend-text">中国台湾</span>
            </div>
            <div class="legend-item">
              <span class="legend-line" style="background: linear-gradient(90deg, cyan 50%, transparent 50%);"></span>
              <span class="legend-text">美国</span>
            </div>
          </div>
          
          <!-- 台风等级 -->
          <div class="legend-group">
            <div class="legend-title">台风等级</div>
            <div class="legend-item">
              <span class="legend-dot" style="background-color: green;"></span>
              <span class="legend-text">热带低压</span>
            </div>
            <div class="legend-item">
              <span class="legend-dot" style="background-color: blue;"></span>
              <span class="legend-text">热带风暴</span>
            </div>
            <div class="legend-item">
              <span class="legend-dot" style="background-color: yellow;"></span>
              <span class="legend-text">强热带风暴</span>
            </div>
            <div class="legend-item">
              <span class="legend-dot" style="background-color: orange;"></span>
              <span class="legend-text">台风</span>
            </div>
            <div class="legend-item">
              <span class="legend-dot" style="background-color: pink;"></span>
              <span class="legend-text">强台风</span>
            </div>
            <div class="legend-item">
              <span class="legend-dot" style="background-color: red;"></span>
              <span class="legend-text">超强台风</span>
            </div>
          </div>
        </div>
        
        <!-- 控制按钮 -->
        <div class="control-buttons">
          <button class="control-btn" @click="renderTyphoon">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M21 12a9 9 0 1 1-6.219-8.56"></path>
            </svg>
            渲染台风
          </button>
          <button class="control-btn" @click="startAnimation">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polygon points="5 3 19 12 5 21 5 3"></polygon>
            </svg>
            开始动画
          </button>
          <button class="control-btn" @click="pauseAnimation">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <rect x="6" y="4" width="4" height="16"></rect>
              <rect x="14" y="4" width="4" height="16"></rect>
            </svg>
            暂停动画
          </button>
          <button class="control-btn" @click="resetAnimation">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M21 2v6h-6M3 10V4a2 2 0 012-2h6M3 22v-6h6M21 14v6a2 2 0 01-2 2h-6"></path>
            </svg>
            重置动画
          </button>
          <button class="control-btn" @click="clearTyphoon">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M3 6h18"></path>
              <path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"></path>
              <path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"></path>
            </svg>
            清除台风
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import { TyphoonUtils } from '../utils/TyphoonUtils';

const props = defineProps({
  viewer: {
    type: Object,
    required: true
  }
});

// 状态管理
const isPanelExpanded = ref(false);
const typhoonUtils = ref(null);
const typhoonData = ref(null);
const isLoading = ref(false);

// 显示选项
const showOptions = ref({
  showWindCircle: true,
  showLandPoints: true,
  showForecastPaths: true,
  showTyphoonPath: true,
  showTyphoonCenter: true,
  showTyphoonIcon: true
});

// 切换面板展开/折叠状态
const togglePanel = () => {
  isPanelExpanded.value = !isPanelExpanded.value;
  if (isPanelExpanded.value) {
    // 面板展开时加载台风数据
    loadTyphoonData();
  } else {
    // 面板折叠时清除台风
    clearTyphoon();
  }
};

// 切换显示选项
const toggleShow = (option) => {
  showOptions.value[option] = !showOptions.value[option];
  renderTyphoon();
};

// 加载台风数据
const loadTyphoonData = async () => {
  if (isLoading.value || typhoonData.value) return;
  
  isLoading.value = true;
  try {
    const response = await fetch('/src/assets/data/typhoon.json');
    typhoonData.value = await response.json();
    
    // 初始化台风工具类
    if (!typhoonUtils.value) {
      typhoonUtils.value = new TyphoonUtils(props.viewer);
    }
    
    // 设置台风数据
    typhoonUtils.value.setTyphoonData(typhoonData.value);
    
    // 渲染台风
    renderTyphoon();
  } catch (error) {
    console.error('Failed to load typhoon data:', error);
  } finally {
    isLoading.value = false;
  }
};

// 渲染台风
const renderTyphoon = () => {
  if (!typhoonUtils.value || !typhoonData.value) return;
  
  // 绘制台风
  typhoonUtils.value.drawTyphoon({
    ...showOptions.value,
    animate: false
  });
};

// 开始动画
const startAnimation = () => {
  if (!typhoonUtils.value || !typhoonData.value) return;
  
  // 绘制台风并启动动画
  typhoonUtils.value.drawTyphoon({
    ...showOptions.value,
    animate: true,
    animationSpeed: 1000 // 1秒/点
  });
};

// 暂停动画
const pauseAnimation = () => {
  if (typhoonUtils.value) {
    typhoonUtils.value.pauseAnimation();
  }
};

// 重置动画
const resetAnimation = () => {
  if (typhoonUtils.value) {
    typhoonUtils.value.resetAnimation();
  }
};

// 清除台风
const clearTyphoon = () => {
  if (typhoonUtils.value) {
    typhoonUtils.value.clearTyphoon();
  }
};

// 监听显示选项变化
watch(showOptions, () => {
  renderTyphoon();
}, { deep: true });

// 组件挂载时初始化
onMounted(() => {
  // 初始化台风工具类
  typhoonUtils.value = new TyphoonUtils(props.viewer);
});

// 组件卸载时清理
onBeforeUnmount(() => {
  clearTyphoon();
});
</script>

<style scoped>
.typhoon-container {
  position: absolute;
  top: 100px;
  right: 20px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.typhoon-control {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  transition: all 0.3s ease;
}

.control-icon {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow:
    0 8px 32px rgba(255, 107, 107, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
  color: white;
}

.control-icon:hover {
  transform: translateY(-2px);
  box-shadow:
    0 12px 40px rgba(255, 107, 107, 0.6),
    0 0 0 1px rgba(255, 255, 255, 0.2);
}

.typhoon-control-panel {
  color: white;
  background: linear-gradient(135deg, rgba(15, 23, 42, 0.15) 0%, rgba(30, 41, 59, 0.15) 100%);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  padding: 12px;
  min-width: 240px;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: all 0.3s ease;
  box-shadow:
    0 10px 25px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(255, 255, 255, 0.08);
}

.typhoon-control.expanded .typhoon-control-panel {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: all;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

.panel-header h3 {
  margin: 0;
  color: white;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  padding-bottom: 0;
  border-bottom: none;
}

.close-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.7);
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

/* 图例样式 */
.legend-section {
  margin-bottom: 16px;
}

.legend-group {
  margin-bottom: 12px;
}

.legend-title {
  color: rgba(255, 255, 255, 0.9);
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.legend-item {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.9);
}

.legend-line {
  width: 30px;
  height: 2px;
  margin-right: 8px;
  display: inline-block;
}

.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 8px;
  display: inline-block;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.legend-text {
  flex: 1;
}

/* 控制按钮样式 */
.control-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.control-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.95);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.control-btn:hover {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  border-color: rgba(255, 255, 255, 0.2);
  color: white;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
  transform: translateX(2px);
}

/* 复选框样式 */
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.95);
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 11px;
  font-weight: 500;
  width: 100%;
}

.checkbox-label:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
}

.legend-checkbox {
  display: none;
}

.checkmark {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  position: relative;
  transition: all 0.2s ease;
}

.checkbox-label input:checked + .checkmark {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  border-color: transparent;
}

.checkbox-label input:checked + .checkmark::after {
  content: '';
  position: absolute;
  left: 5px;
  top: 2px;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
</style>